<template>
  <div class="questions-preview">
    <el-dialog
      title="提示"
      :visible.sync="isEdit"
      width="50%"
      :before-close="handleClose"
    >
      <el-row>
        <el-col :span="6">【题型】：{{myData.questionType|questionTypeTrans}}</el-col>
        <el-col :span="6">【编号】：{{myData.id}}</el-col>
        <el-col :span="6">【难度】：{{myData.difficulty|difficultyTrans}}</el-col>
        <el-col :span="6">【标签】：{{myData.tags}}</el-col>
        <el-col :span="6">【学科】：{{myData.subjectName}}</el-col>
        <el-col :span="6">【目录】：{{myData.directoryName}}</el-col>
        <el-col :span="6">【方向】：{{myData.direction}}</el-col>
      </el-row>
      <hr>
      【题干】：<div v-html="myData.question" style="color:blue"></div>
      <div v-if="myData.questionType!=='3'">
        <div style="padding-bottom:5px">{{myData.questionType|questionTypeTrans}} 选项：（以下选中的选项为正确答案）</div>
        <div v-for="item in myData.options" :key="item.code" style="padding:8px 0">
          <el-radio v-if="myData.questionType==='1'" :value="item.isRight" :label="1">{{item.title}}</el-radio>
          <el-checkbox v-if="myData.questionType==='2'" :value="item.isRight?true:false" >{{item.title}}</el-checkbox>
        </div>
      </div>
      <hr>
      【参考答案】：<el-button type="danger" size="small" @click="preivewVideo()">视频答案预览</el-button>
      <div class="video" v-show="isVideo">
        <video ref="video" :src="myData.videoURL" controls></video>
      </div>
      <hr>
      【答案解析】：<div style="display:inline-block" v-html="myData.answer"></div>
      <hr>
      【题目备注】：{{myData.remarks}}
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { difficulty, questionType } from '@/api/hmmm/constants'
import { detail } from '@/api/hmmm/questions'
export default {
  name: 'QuestionPreview',
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      isEdit: false, // 预览弹框显示与隐藏
      myData: {}, // 预览题目详情
      isVideo: false // 视频的显示与隐藏
    }
  },
  filters: {
    // 数据转换
    difficultyTrans (value) {
      const difficultyList = difficulty.filter((item) => item.value === value)
      return difficultyList.map((item) => item.label)[0]
    },

    questionTypeTrans (value) {
      const questionTypeList = questionType.filter(
        (item) => item.value === value
      )
      return questionTypeList.map((item) => item.label)[0] + '题'
    }
  },
  methods: {
    // 视频播放
    preivewVideo () {
      this.isVideo = true
      // play()播放返回promise对象，播放失败会返回一个错误信息
      this.$refs.video.play().catch(err => err)
    },

    // 打开事件
    async open () {
      this.isEdit = true
      const res = await detail({ id: this.data.id })
      this.myData = res.data
    },

    // 关闭按钮
    handleClose () {
      this.isEdit = false
      // pause()暂停
      this.$refs.video.pause()
      this.isVideo = false
    }
  }
}
</script>

<style scoped lang="scss">
.questions-preview{
  .el-col{
    padding: 10px 0;
  }
}
.video{
  width: 400px;
  height: 300px;
  margin: 20px 0;
  video {
    width: 100%;
    height: 100%;
  }
}
</style>
